package ma.fsm.arda;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;

public class Server {

	private static URI getBaseURI() {
		// sakina
		// String localhost = "" ;

		try {
			// Properties prop = new Properties();
			// prop.load(new FileInputStream("config.properties"));
			// if (prop.getProperty("ip").equals("0")) {
			// localhost = Inet4Address.getLocalHost().toString().split("/")[1];
			// } else {
			// localhost = prop.getProperty("ip");
			// }
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		// return UriBuilder.fromUri("http://" + localhost + "/").port(9998)
		// .build();
		// Static IP+
		return UriBuilder.fromUri("http://172.16.14.10/").port(9998).build();
	}

	public static final URI BASE_URI = getBaseURI();

	protected static HttpServer startServer() throws IOException {
		System.out.println("Starting grizzly...");
		ResourceConfig rc = new PackagesResourceConfig("ma.fsm.arda.service");
		return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
	}

	public static void main(String[] args) throws IOException {
		HttpServer httpServer = startServer();
		System.out.println(String.format("server", BASE_URI, BASE_URI));
		System.out.println(System.getProperty("os.name"));
		System.out.println(System.getProperty("os.arch"));
		System.in.read();
		httpServer.stop();
	}
}
